package com.cssnj.portal.config;

import io.predictech.framework.utility.LogUtil;
import io.predictech.framework.utility.PredictechUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;

import javax.sql.DataSource;


@Configuration
public class SecondDataSourceConfig {

    @Bean
    @ConfigurationProperties("spring.second.datasource")
    public DataSourceProperties secondDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(destroyMethod = "")
    @ConfigurationProperties("spring.second.datasource")
    public DataSource secondDataSource() {
        LogUtil.systemLogger().info("Registering Secondary Data Source");
        DataSourceProperties dataSourceProperties = secondDataSourceProperties();
        return PredictechUtil.getDataSource(dataSourceProperties);
    }

    @Bean(name = "secondJdbcTemplate")
    public JdbcTemplate secondJdbcTemplate(@Qualifier("secondDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
